# encoding: utf-8
# @author: wangxin
# @software: garner
# @file: OrderSettingviews.py
# @time: 2025/1/23 10:03
from calendar import month
from turtledemo.penrose import start

import pymysql
from django.db.models.expressions import result
from django.views import View
import json
from tabnanny import check

# 导入Django核心模块中的序列化器，用于将Django模型转换为其他格式，如JSON、XML等
from django.core import serializers

# 导入Django序列化功能，以便在需要时将模型序列化
from django.core.serializers import serialize
from django.db.models import Q

# 导入HttpResponse和JsonResponse类，用于构建HTTP响应和JSON响应
from django.http import HttpResponse, JsonResponse

# 导入render函数，它用于根据请求和模板渲染网页
from django.shortcuts import render
import time

from django.views import View

from backend.Maxday import get_max_days_in_month


class OrderSettingView(View):
    def get(self,request):
        initData=request.GET.get('initData')
        year=initData.split('-')[0]
        month=initData.split('-')[1]
        print(year,month)
        month_day=get_max_days_in_month(year,month)

        #拼接日期
        stattime=initData+"-"+"01"
        endtime=initData+"-"+str(month_day)

        conn = pymysql.Connect(
            host="127.0.0.1",
            port=3306, user="root",
            password="123456",
            db="ruici",
            charset="utf8",
            autocommit=True
        )
        cursor = conn.cursor()
        sql = f"SELECT * FROM t_ordersetting where orderDate > '{stattime}' AND orderDate < '{endtime}' ;"
        cursor.execute(sql)
        res = cursor.fetchall()
        print(res)
        data_list=list()
        for data in res:
            data_dict=dict()
            data_dict['date']=data[1].day
            data_dict['number']=data[2]
            data_dict['reservations']=data[3]
            data_list.append(data_dict)
        print(data_list)
        result={
            'code':200,
            'data':data_list
        }


        return JsonResponse(result)

    def  put(self, request):
        data = request.GET.get('setOderDate')
        number=request.GET.get('number')
        conn = pymysql.Connect(
            host="127.0.0.1",
            port=3306, user="root",
            password="123456",
            db="ruici",
            charset="utf8",
            autocommit=True
        )
        cursor = conn.cursor()
        sqlfind=f"SELECT * FROM t_ordersetting where orderDate = '{data}' ;"
        cursor.execute(sqlfind)
        res = cursor.fetchone()
        print(res)

        if res==None:
            sql = f"INSERT INTO t_ordersetting (orderDate,number,reservations) VALUES ('{data}',{number},0);"
            cursor.execute(sql)

        sql = f"UPDATE t_ordersetting SET number = {number} WHERE orderDate = '{data}'; ;"
        cursor.execute(sql)
        res = cursor.fetchall()
        print(res)

        return HttpResponse("ok")
class uploadExcel(View):
    def post(self,request):
        return HttpResponse("ok")
    def get(self,request):
        return HttpResponse("ok")

